import request from '@/utils/request';
import { requestWrapper, randomInt, randomDate, paginate } from '@/utils/mock';

// 获取备份列表
export function getBackupList(params) {
  return requestWrapper(
    // 真实API请求
    () => request({
      url: '/backup/list',
      method: 'get',
      params
    }),
    // 模拟数据
    () => {
      const pageNum = params?.pageNum || 1;
      const pageSize = params?.pageSize || 10;
      const total = 25; // 模拟总数
      
      // 备份类型
      const backupTypes = ['数据库', '文件', '配置', '全量'];
      // 备份状态
      const statusOptions = ['0', '1', '2']; // 0-失败 1-成功 2-进行中
      
      // 生成备份数据
      const generateBackup = (index) => {
        const backupId = index + 1;
        const backupType = backupTypes[randomInt(0, backupTypes.length - 1)];
        const status = statusOptions[randomInt(0, statusOptions.length - 1)];
        const createTime = randomDate(-30);
        
        return {
          backupId,
          backupName: `备份_${createTime.split(' ')[0].replace(/-/g, '')}_${backupId}`,
          backupType,
          fileSize: `${randomInt(1, 500)}MB`,
          status,
          remark: status === '1' ? '备份成功' : (status === '0' ? '备份失败' : '备份进行中'),
          createBy: 'admin',
          createTime
        };
      };
      
      // 模拟分页
      const startIndex = (pageNum - 1) * pageSize;
      const endIndex = Math.min(startIndex + pageSize, total);
      const list = Array.from({ length: endIndex - startIndex }, (_, i) => generateBackup(startIndex + i));
      
      return paginate(list, pageNum, pageSize, total);
    }
  );
}

// 创建备份
export function createBackup(data) {
  return requestWrapper(
    // 真实API请求
    () => request({
      url: '/backup/create',
      method: 'post',
      data
    }),
    // 模拟数据
    {
      backupId: randomInt(100, 999),
      backupName: `备份_${new Date().toISOString().split('T')[0].replace(/-/g, '')}_${randomInt(100, 999)}`,
      message: '备份任务已提交，请稍后查看结果'
    }
  );
}

// 恢复备份
export function restoreBackup(backupId) {
  return requestWrapper(
    // 真实API请求
    () => request({
      url: `/backup/restore/${backupId}`,
      method: 'post'
    }),
    // 模拟数据
    {
      message: '恢复任务已提交，请稍后查看结果'
    }
  );
}

// 删除备份
export function deleteBackup(backupIds) {
  return requestWrapper(
    // 真实API请求
    () => request({
      url: `/backup/${backupIds}`,
      method: 'delete'
    }),
    // 模拟数据
    null
  );
}

// 下载备份
export function downloadBackup(backupId) {
  return requestWrapper(
    // 真实API请求
    () => request({
      url: `/backup/download/${backupId}`,
      method: 'get',
      responseType: 'blob'
    }),
    // 模拟数据
    () => new Blob(['模拟备份文件内容'], { type: 'application/octet-stream' })
  );
}

// 获取备份配置
export function getBackupConfig() {
  return requestWrapper(
    // 真实API请求
    () => request({
      url: '/backup/config',
      method: 'get'
    }),
    // 模拟数据
    {
      autoBackup: true,
      backupTime: '02:00',
      backupCycle: 'daily', // daily, weekly, monthly
      backupRetention: 7,
      backupPath: '/data/backup',
      backupItems: ['database', 'file', 'config']
    }
  );
}

// 更新备份配置
export function updateBackupConfig(data) {
  return requestWrapper(
    // 真实API请求
    () => request({
      url: '/backup/config',
      method: 'put',
      data
    }),
    // 模拟数据
    null
  );
} 